﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using D = System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using Microsoft.Office.Interop.Excel;

namespace ExcelTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private D::DataTable readExcelSheet(string path)
        {
            // string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
            string strConn = "Provider = Microsoft.ACE.OLEDB.12.0 ; Data Source = " + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1;\"";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();

            D::DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
            /**
                获取 excel 中工作簿信息: schematable 常用字段 table_name,date_modified,date_created
              
                schemaTable.DefaultView.Sort = "date_modified desc";
                schemaTable = schemaTable.DefaultView.ToTable();
                string tableName = schemaTable.Rows[0]["table_name"].ToString().Trim();
             */

            string strExcel = "select * from [sheet1$]";
            OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);

            var dt = new D::DataTable();
            myCommand.Fill(dt);
            return dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // ofd.ShowDialog();

            Stream myStream = null;
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.InitialDirectory = "c:\\";
            ofd.Filter = "excel |*.xls;*.xlsx";
            ofd.FilterIndex = 0;
            ofd.RestoreDirectory = true;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = ofd.OpenFile()) != null)
                    {
                        myStream.Close();
                        var dt = readExcelSheet(ofd.FileName);
                        dataGridView1.DataSource = dt;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {

            ApplicationClass excel = new ApplicationClass();

            Workbook book = excel.Workbooks.Add(true);
            Worksheet sheet = (Worksheet)book.ActiveSheet;

            D::DataTable dt = (D::DataTable)dataGridView1.DataSource;
            int row = 1;
            int col = 1;
            foreach (D::DataColumn dc in dt.Columns)
            {
                sheet.Cells[row, col] = dc.ColumnName;
                col++;
            }
            row++;

            foreach (D::DataRow dw in dt.Rows)
            {
                for (col = 1; col < dt.Columns.Count; col++)
                {
                    sheet.Cells[row, col] = dw[col-1];
                }
                row++;
            }


            excel.Visible = true;

        }
    }
}
